package com.hongxun.budget.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.hongxun.interfaces.NovaCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import dmax.dialog.SpotsDialog;
import me.shaohui.advancedluban.Luban;
import me.shaohui.advancedluban.OnCompressListener;
import me.shaohui.advancedluban.OnMultiCompressListener;

public class BitmapPressUtils {

    static SpotsDialog dialog;

    /**
     * 压缩多种图片
     *
     * @param context
     * @param paths
     * @param c
     */
    public static void pressList(final Context context, List<String> paths, final NovaCallback c) {
        List<File> files = new ArrayList<>();
        for (String s : paths) {
            File file = new File(s);
            files.add(file);
        }
        dialog = new SpotsDialog(context, "图片压缩中....");
        Luban.compress(context, files).setMaxSize(50).setMaxHeight(800).setMaxWidth(480)
                .putGear(Luban.CUSTOM_GEAR)
                .launch(new OnMultiCompressListener() {
                    @Override
                    public void onStart() {
                        dialog.show();
                    }

                    @Override
                    public void onSuccess(List<File> fileList) {
                        List<String> strings = new ArrayList<>();
                        for (File f : fileList) {
                            strings.add(f.getAbsolutePath());
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("paths", JSONObject.toJSONString(strings));
                        c.success(888, jsonObject);
                        dialog.cancel();
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showLong(context, e.getMessage());
                        dialog.cancel();
                    }
                });
    }

    /**
     * 压缩单张图片
     *
     * @param context
     * @param path
     * @param c
     */
    public static void press(final Context context, String path, final NovaCallback c) {
        dialog = new SpotsDialog(context, "图片压缩中....");
        Luban.compress(context, new File(path)).setMaxSize(50).setMaxHeight(800).setMaxWidth(480)
                .putGear(Luban.CUSTOM_GEAR)
                .launch(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        dialog.show();
                    }

                    @Override
                    public void onSuccess(File file) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("path", file.getAbsolutePath());
                        c.success(889, jsonObject);
                        dialog.cancel();
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showLong(context, e.getMessage());
                        dialog.cancel();
                    }
                });
    }

}
